package com.twotwoshop.service.order.controller;

import com.twotwoshop.domin.common.dtos.ResponseResult;
import com.twotwoshop.domin.common.enums.AppHttpCodeEnum;
import com.twotwoshop.domin.order.dtos.OrderDto;
import com.twotwoshop.domin.order.dtos.OrderFindDto;
import com.twotwoshop.service.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;



    /**
     * 获取下单令牌
     */
    @GetMapping("/token")
    public ResponseResult getOrderToken() {
        return orderService.generateOrderToken();
    }


    /**
     *
     * 下单
     *
     * @param
     * @return
     */
    @PostMapping("/create")
    public ResponseResult downOrder(@RequestBody OrderDto orderDto){

        if (StringUtils.isEmpty(orderDto.getIdempotentToken())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "令牌不能为空");
        }

        log.info("下单...{}");

        return orderService.downOrder(orderDto);
    }



    /**
     *
     * 获取订单列表
     *
     * @param
     * @return
     */
    @PostMapping("/my/list")
    public ResponseResult getOrderList(@RequestBody OrderFindDto orderFindDto){

        log.info("获取订单列表...{}");

        return orderService.getOrderList(orderFindDto);
    }


    /**
     *
     * 取消订单
     *
     * @param
     * @return
     */
    @PostMapping("/cancel/{id}")
    public ResponseResult cancelOrder(@PathVariable("id") Long id){

        log.info("取消订单...{}");

        return orderService.cancelOrder(id);
    }




    /**
     *
     * 删除订单
     *
     * @param
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseResult deleteOrder(@PathVariable("id") Long id){

        log.info("删除订单...{}");

        return orderService.deleteOrder(id);
    }


    /**
     *
     * 查询我卖出的商品
     *
     * @param
     * @return
     */
    @PostMapping("/sold/list")
    public ResponseResult mySoldOrder(@RequestBody OrderFindDto orderFindDto){

        log.info("查询我卖出的商品...{}");

        return orderService.mySoldOrder(orderFindDto);
    }


    /**
     *
     * 发货
     *
     * @param
     * @return
     */
    @PostMapping("/ship/{id}")
    public ResponseResult shipOrder(@PathVariable Long id){

        log.info("发货...{}");

        return orderService.shipOrder(id);
    }



    /**
     *
     * 确认收货
     *
     * @param
     * @return
     */
    @PostMapping("/confirm/{id}")
    public ResponseResult confirmOrder(@PathVariable Long id){

        log.info("确认收货...{}");

        return orderService.confirmOrder(id);
    }




    /**
     *
     * 买家申请退款
     *
     * @param
     * @return
     */
    @PostMapping("/apply/refund/{id}")
    public ResponseResult refundOrder(@PathVariable("id") Long id){

        log.info("买家申请退款...{}");

        return orderService.refundOrder(id);
    }


    /**
     *
     * 卖家同意退款
     *
     * @param
     * @return
     */
    @PostMapping("/refund/agree/{id}")
    public ResponseResult refundAgreeOrder(@PathVariable("id") Long id){

        log.info("卖家同意退款...{}");

        return orderService.refundAgreeOrder(id);
    }



    /**
     *
     * 卖家拒绝退款
     *
     * @param
     * @return
     */
    @PostMapping("/refund/reject/{id}")
    public ResponseResult refundRejectOrder(@PathVariable("id") Long id){

        log.info("卖家拒绝退款...{}");

        return orderService.refundRejectOrder(id);
    }


}
